function backUserManagementAdd() {
	window.location.href = 'userManagement.jsp';
}

var somearray = [];

function removeByValue(arr, val) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
}

function checka(data) {
	var tickBlock = document.getElementById(data.id);
	if(tickBlock.checked == true) {
		removeByValue(somearray, data.id);
		somearray.push(data.id);

	} else if(tickBlock.checked == false) {
		removeByValue(somearray, data.id);
	}
	for(var i = 0; i < somearray.length; i++) {
		if(somearray[i] == 1) {
			if(somearray.length > 1) {
				alert("超级管理员和其他管理员不能一起选择！请重选！");
				var id = document.getElementById(1);
				id.checked = false;
				removeByValue(somearray, 1);

			}
		}
	}
}

function checkdiv(data) {
	$(".userManagementAddInput").val(data.id);

}

function userManagementAddInput() {
	$(".manageAdd_content_d_content_input_div").show()
}
$(".manageAdd_content_d_content_input_div").mouseleave(function() {
	$(".manageAdd_content_d_content_input_div").hide()
});

function formform() {
	$('#form1')[0].reset();
	$(':input', '#form1').removeAttr('checked');
}

var param = {
	userId: $.Cookie("userId"),
	session: $.Cookie("session")
}
$.ajax({
	url: 'roleList',
	type: 'POST',
	data: param,
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			$.ajax({
				url: 'orgListTree',
				type: 'POST',
				data: {
					companyId: $.Cookie("companyId"),
					session: $.Cookie("session"),
					userId: $.Cookie("userId"),
				},
				dataType: 'json',
				success: function(data) {
					var setting = {
						data: {

							simpleData: {
								enable: true
							}
						},
						callback: {
							beforeClick: beforeClick

						}
					};

					var zNodes = data.result

					var log, className = "dark";

					function beforeClick(treeId, treeNode, clickFlag) {
						$(".userManagementAddInput").attr("value", treeNode.name); //填充内容
						$(".userManagementAddInput").attr("name", treeNode.orgId); //填充内容
					}

					$(document).ready(function() {
						$.fn.zTree.init($("#dvcGroupTree"), setting, zNodes);
					});
				},
				error: function(err) {
					alert("连接服务器失败！请重新提交！");
					top.location.href = 'index.jsp';
				}
			});
			var superRoleId = sessionStorage.getItem("superRoleId");
			$.each(data.result, function(i, value) {
				var error_tpl;
				if(superRoleId == 1) {
					error_tpl = '<div><input type="checkbox" name="checklist1" onclick="checka(this)" value="' + value.roleName + '" id="' + value.id + '"><label for="' + value.id + '">' + value.roleName + '</label> </div>'
				} else {
					if(i == 0) {

					} else {
						error_tpl = '<div><input type="checkbox" name="checklist1" onclick="checka(this)" value="' + value.roleName + '" id="' + value.id + '"><label for="' + value.id + '">' + value.roleName + '</label> </div>'

					}
				}

				$(".userManagementAdd_role").append(error_tpl)
			});
		} else if(data.errorCode == "-1") {
			alert("系统异常请重试")
		} else if(data.errorCode == "-2") {
			alert("没有全部填完！")
		} else if(data.errorCode == "-6") {
			alert("登录超时！")
			top.location.href = 'index.jsp';
		}
	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！");
		top.location.href = 'index.jsp';

	}
});

function submitData() {

	a = somearray.join(",");
	$.ajax({
		url: 'addUser',
		type: 'POST',
		data: {
			companyId: $.Cookie("companyId"),
			session: $.Cookie("session"),
			userId: $.Cookie("userId"),
			userCode: $("#userCode").val(),
			userName: $("#userCode").val(),
			userPwd: hex_md5($("#userPwd").val()),
			orgId: $(".userManagementAddInput").attr("name"),
			roleId: a,
			email: $("#email").val(),
			phone: $("#phone").val(),
		},
		dataType: 'json',
		success: function(data) {
			if(data.errorCode == 0) {
				window.location.href = 'userManagement.jsp';
			} else if(data.errorCode == -6) {
				alert("登录超时！")
				top.location.href = 'index.jsp';
			}
		},
		error: function(err) {
			alert("连接服务器失败！请重新提交！");
			top.location.href = 'index.jsp';

		}
	});
}